package com.example.thread.base.demo4;

import lombok.extern.slf4j.Slf4j;

import java.util.LinkedList;
import java.util.Queue;


/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2020/12/16 9:28
 */
@Slf4j
public class demo4_1 {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue queue = new BlockingQueue();
        String s=queue.take();
        queue.give("wait错误使用");
    }
}

class BlockingQueue {
    Queue<String> buffer = new LinkedList<String>();

    public void give(String data) {
        synchronized (this) {
            buffer.add(data);
            notify();
        }
    }

    public String take() throws InterruptedException{
        synchronized (this) {
            while (buffer.isEmpty()) {
                wait();
            }
            return buffer.remove();
        }
    }

}